<template>
  <div class="goodsinfo-container">
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>
    <!-- 商品轮播图区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :lunbotu="lunbolist" :isfull="false"></swiper>
        </div>
      </div>
    </div>
    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{ goodsinfo["0"].title }}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价:
            <del>￥{{ goodsinfo["0"].market_price }}</del>
            &nbsp;&nbsp;销售价:
            <span class="now_price">{{ goodsinfo["0"].sell_price }}</span>
          </p>
          <p>
            购买数量:
            <numbox @getnum="getNum" :max="goodsinfo[0].stock_quantity"></numbox>
          </p>
          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="add">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>
    <!-- 商品参数区域 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号:{{ goodsinfo["0"].goods_no }}</p>
          <p>库存情况:{{ goodsinfo["0"].stock_quantity }}</p>
          <p>上架时间:{{ goodsinfo["0"].add_time | dateFormat }}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goComments(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
import swiper from "../subcomponents/swiper.vue";

import numbox from "../subcomponents/goodsinfo_numbox.vue";
export default {
  data() {
    return {
      lunbolist: [],
      id: this.$route.params.id,
      goodsinfo: {},
      ballFlag: false,
      num: 1
    };
  },
  created() {
    this.getlunbo();
    this.getGoodsInfo();
  },
  methods: {
    getlunbo() {
      this.$http.get("api/getthumimages/" + this.id).then(result => {
        if (result.body.status === 0) {
          result.body.message.forEach(item => {
            item.img = item.src;
          });
          this.lunbolist = result.body.message;
        }
      });
    },
    getGoodsInfo() {
      this.$http.get("api/goods/getinfo/" + this.id).then(result => {
        if (result.body.status === 0) {
          this.goodsinfo = result.body.message;
        }
      });
    },
    goDesc(id) {
      this.$router.push({ name: "goodsdesc", params: { id } });
    },
    goComments(id) {
      this.$router.push({ name: "goodscomments", params: { id } });
    },
    add() {
      this.ballFlag = !this.ballFlag;
      // {id：商品id,count:购买数量,price:单价,selected：是否选中}
      // 拼接出一个，要保存到store仓库 中car数组的商品信息对象
      var goodsinformation = {
        id: this.id,
        count: this.num,
        price: this.goodsinfo[0].sell_price,
        selected: true
      };

      // 调用store中的mytations中的方法，将商品加入购物车
      this.$store.commit("addToStore", goodsinformation);
    },
    beforeEnter: el => {
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      el.offsetWidth;

      // 获取小球在页面位置
      const ballPosition = this.$refs.ball.getBoundingClientRect();
      // 获取徽标在页面中的位置
      const badgePosition = document
        .querySelector("#badge")
        .getBoundingClientRect();

      const x = badgePosition.left - ballPosition.left;
      const y = badgePosition.top - ballPosition.top;

      el.style.transform = `translate(${x}px,${y}px)`;
      el.style.transition = "all 0.5s cubic-bezier(.83,-0.36,.99,.32) ";
      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },
    getNum(number) {
      this.num = number;
      console.log("父组件拿到的值为:" + this.num);
    }
  },
  components: {
    swiper,
    numbox
  }
};
</script>

<style lang="scss" scoped>
.goodsinfo-container {
  background-color: lightgray;
  overflow: hidden;
  .now_price {
    color: red;
    font-size: 16px;
    font-weight: bold;
  }
  .mui-card-footer {
    display: block;
    button {
      margin: 15px 0;
    }
  }
  .ball {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    z-index: 999;
    top: 395px;
    left: 142px;
  }
}
</style>

